package com.usdt.system.service.impl.cp;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usdt.common.exception.ServiceException;
import com.usdt.common.utils.bean.BeanUtil;
import com.usdt.system.cp.utils.UserAndDeptUtils;
import com.usdt.system.domain.cp.ActivityTempEntity;
import com.usdt.system.domain.req.ActivityTempReqVO;
import com.usdt.system.domain.req.ActivityTemplistExportReqVO;
import com.usdt.system.domain.req.ActivityUpdateStatusReqVO;
import com.usdt.system.domain.resp.ActivityTempRespVO;
import com.usdt.system.dto.cp.ActivityTempListExcelDTO;
import com.usdt.system.mapper.cp.ActivityTempMapper;
import com.usdt.system.service.ISysDictDataService;
import com.usdt.system.service.cp.ActivityTempService;

@Service
public class ActivityTempServiceImpl extends ServiceImpl<ActivityTempMapper, ActivityTempEntity> 
										implements ActivityTempService{
	@Value("${sys.properties.ky.main_dept_id}")
	public String mainDeptId;
	
	@Autowired
	private ISysDictDataService sysDictDataService;
	
	@Override
	public Integer insert(ActivityTempReqVO vo) {
		
		ActivityTempEntity activityTempEntity = BeanUtil.toBean(vo, ActivityTempEntity.class);
		if(!CollectionUtils.isEmpty(vo.getActivityProcessDescription())) {
			activityTempEntity.setActivityProcessDescriptionJson(JSON.toJSONString(vo.getActivityProcessDescription()));
		}
		Map<String, String> currentUserIdAndDeptId = UserAndDeptUtils.getCurrentUserIdAndDeptId();
		activityTempEntity.setDeptId(currentUserIdAndDeptId.get("currentDeptId"));
		activityTempEntity.setCreateBy(currentUserIdAndDeptId.get("currentUserId"));
		activityTempEntity.setUpdateBy(currentUserIdAndDeptId.get("currentUserId"));
		activityTempEntity.setActivityDurationMin(this.getActivityDurationMin(activityTempEntity.getActivityDuration(),activityTempEntity.getActivityDurationUnit()));
		baseMapper.insert(activityTempEntity);
		return activityTempEntity.getId();
	}

	@Override
	public ActivityTempRespVO detail(Integer id) {
		 ActivityTempEntity activityTempEntity =selectById(id);
		ActivityTempRespVO result = BeanUtil.toBean(activityTempEntity,ActivityTempRespVO.class);
		//sysDictDataService.selectDictLabel(DOCT_TYPE, activityTempEntity.getDeptId());
		return result;
	}
	
	@Override
	public ActivityTempEntity selectById(Integer id) {
		return baseMapper.selectById(id);
	}

	@Override
	public IPage<ActivityTempRespVO> list(Integer activityCategory, Integer activityType, String keyWord,String name, 
			Byte status,String deptId,BigDecimal activityDurationMin,BigDecimal activityDurationMax,
			Integer pageNum,Integer pageSize) {
		
		QueryWrapper<ActivityTempEntity> query = new QueryWrapper<ActivityTempEntity>();
		query.orderByDesc("id");
		if(null!=activityCategory&&-1!=activityCategory) {
			query.eq("activity_category", activityCategory);
		}
		if(null!=activityType&&-1!=activityType) {
			query.eq("activity_type", activityType);
		}
		if(null!=status) {
			query.eq("status", status);
		}
		if(null!=deptId) {
			query.eq("dept_id", deptId);
		}
		if(StringUtils.isNotBlank(keyWord)) {
			query.and(i -> i.like("name", keyWord).or().like("activty_taboo", keyWord));
		}
		if(StringUtils.isNotBlank(name)) {
			query.like("name", name);
		}
		if(null!=activityDurationMin) {
			query.ge("activity_duration_min", activityDurationMin);
		}
		if(null!=activityDurationMax) {
			query.le("activity_duration_min", activityDurationMax);
		}
		
		Page<ActivityTempEntity> page = baseMapper.selectPage(new Page<>(pageNum, pageSize), query);
		
		return page.convert(e->{
			ActivityTempRespVO bean = BeanUtil.toBean(e, ActivityTempRespVO.class);
			return bean;
		});
	}
	
	@Override
	public List<ActivityTempRespVO> allList(String name) {
		List<ActivityTempRespVO> result = new ArrayList<ActivityTempRespVO>();
		
		QueryWrapper<ActivityTempEntity> query = new QueryWrapper<ActivityTempEntity>();
		query.eq("status", (byte)1).orderByDesc("id");
		if(StringUtils.isNotBlank(name)) {
			query.like("name", name);
		}
		List<ActivityTempEntity> list = baseMapper.selectList(query);
		if(!CollectionUtils.isEmpty(list)) {
			result = BeanUtil.toBeans(list, ActivityTempRespVO.class);
		}
		return result;
	}

	@Override
	public Integer update(ActivityTempReqVO vo) {
		ActivityTempEntity activityTempEntity = BeanUtil.toBean(vo, ActivityTempEntity.class);
		if(!CollectionUtils.isEmpty(vo.getActivityProcessDescription())) {
			activityTempEntity.setActivityProcessDescriptionJson(JSON.toJSONString(vo.getActivityProcessDescription()));
		}
		Map<String, String> currentUserIdAndDeptId = UserAndDeptUtils.getCurrentUserIdAndDeptId();
		activityTempEntity.setUpdateBy(currentUserIdAndDeptId.get("currentUserId"));
		activityTempEntity.setActivityDurationMin(this.getActivityDurationMin(activityTempEntity.getActivityDuration(),activityTempEntity.getActivityDurationUnit()));
		baseMapper.updateById(activityTempEntity);
		return vo.getId();
	}

	@Override
	public Integer updateStatus(ActivityUpdateStatusReqVO vo) {
		Map<String, String> currentUserIdAndDeptId = UserAndDeptUtils.getCurrentUserIdAndDeptId();
		String currentDeptId = currentUserIdAndDeptId.get("currentDeptId");
		ActivityTempEntity selectById = baseMapper.selectById(vo.getId());
		if(!currentDeptId.equals(selectById.getDeptId()) && !mainDeptId.equals(currentDeptId)) {
			throw new ServiceException("非本项目员工不允许弃用/恢复使用本项目的活动模板", -1);
		}
		
		ActivityTempEntity activityTempEntity = new ActivityTempEntity();
		activityTempEntity.setId(vo.getId());
		activityTempEntity.setStatus(vo.getStatus());
		activityTempEntity.setUpdateBy(currentUserIdAndDeptId.get("currentUserId"));
		baseMapper.updateById(activityTempEntity);
		return vo.getId();
	}
	
	@Override
	public List<ActivityTempListExcelDTO> listExport(ActivityTemplistExportReqVO vo) {
		List<ActivityTempListExcelDTO> result = baseMapper.selectList1(vo.getActivityCategory(),
				vo.getActivityType(),
				vo.getKeyWord()
				,vo.getStatus(),
				vo.getActivityDurationMin(),
				vo.getActivityDurationMax(),
				vo.getDeptId());
		
		return CollectionUtils.isEmpty(result)?new ArrayList<ActivityTempListExcelDTO>():result;
	}
	
	//活动时长转化为分钟
	private BigDecimal getActivityDurationMin(BigDecimal activityDuration, Integer activityDurationUnit) {
		if(null==activityDuration) {
			return activityDuration;
		}
		if(1==activityDurationUnit){//分钟
			return activityDuration;
		}
		if(2==activityDurationUnit){//小时
			return activityDuration.multiply(new BigDecimal("60"));
		}
		if(3==activityDurationUnit){//天
			return activityDuration.multiply(new BigDecimal("24").multiply(new BigDecimal("60")));
		}
		return activityDuration;
	}

}
